package com.example.test.advice.第三章_类_对象_方法.建议47_在equals中使用getClass进行类型判断;

public class EqualsClass {

    public static void main(String[] args) {
        Employee e1 = new Employee("张三",100);
        Employee e2 = new Employee("张三",101);
        Person p1  = new Person("张三");
        System.out.println(p1.equals(e1));
        System.out.println(p1.equals(e2));
        System.out.println(e1.equals(e2));

    }
}

class Employee extends Person{

    private int id;

    public Employee(String name,int id){
        super(name);
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if(o instanceof Employee){
            Employee e = (Employee) o;
            return super.equals(o)&&e.getId() == id;
        }

        return false;
    }

}


class Person{

    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if(o!=null && o.getClass()==this.getClass()){
            Person p = (Person) o;
            if(p.getName()==null || name == null){
                return false;
            }else {
                return name.equalsIgnoreCase(p.getName());
            }
        }
        return false;
    }
}